package com.gurun.camera.device;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.danale.player.listener.MediaState;
import com.danale.video.sdk.player.DanalePlayer;
import com.gurun.camera.BaseActivity;
import com.gurun.camera.device.presenter.IVideoPresenter;
import com.gurun.camera.device.view.ILivePlayView;
import com.gurun.camera.utils.ToastUtil;

public class BaseVideoActivity extends BaseActivity implements ILivePlayView {

    protected IVideoPresenter videoPresenter;

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
        if (videoPresenter != null){
            videoPresenter.prepare();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    videoPresenter.startVideo();
                }
            }, 200);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoPresenter != null){
            videoPresenter.release();  // stop some operation and release some sources
        }
        unRegisterReceiver();
    }

    public void onClickBack(View view){
        finish();
    }

    /**
     * control you mobile-phone to record or stop record.
     * @param view
     */
    public void onClickRecord(View view){
        videoPresenter.clickRecord();
    }

    /**
     * snapshot the screen of playing video
     * @param view
     */
    public void onClickSnapshot(View view){
        videoPresenter.capture();
    }

    /**
     * control the operation of talk.
     * you can talk on mobile-phone, and others can get you voice from IPCamera.
     *
     * start or stop
     *
     * @param view
     */
    public void onClickTalk(View view){
        videoPresenter.clickTalk();
    }

    /**
     * control the operation of starting and stopping audio.
     * you can get the voice of IPCamera.
     *
     * @param view
     */
    public void onClickAudio(View view){
        videoPresenter.clickAudio();
    }

    /**
     * the callback of video state
     *
     * @param id
     * @param state
     */
    @Override
    public void showVideoState(String id, MediaState state) {
        switch (state){
            case STARTED:
                Log.d("info","video loading start");
                break;
            case START_FAIL:
                Log.d("info","video loading failed");
                break;
            case RUNNING:
                Log.d("info","video loading successfully");
                break;
            case STOPPED:
                Log.d("info","Video stop");
                break;
            case TIME_OUT:
                Log.d("info","Video load timeout");
                break;
        }
    }

    /**
     * the callback of audio state
     * @param state audio state
     */
    @Override
    public void showAudioState(MediaState state) {
        switch (state){
            case STARTED:
                Log.d("info","Audio is loading");
                break;
            case RUNNING:
                Log.d("info","Audio loading successfully");
                break;
            case START_FAIL:
                Log.d("info","Audio loading failed");
                break;
            case IDLE:
                Log.d("info","Audio stop");
                break;
        }
    }

    /**
     * the callback of talk state
     * @param state talk state
     */
    @Override
    public void showTalkState(MediaState state) {
        switch (state){
            case STARTED:
                Log.d("info","Talk is loading");
                break;
            case RUNNING:
                Log.d("info","Talk loading successfully");
                break;
            case START_FAIL:
                Log.d("info","Talk loading failed");
                break;
            case IDLE:
                Log.d("info","Talk stop");
                break;
        }
    }

    /**
     * the callback of record state
     * @param state record state
     * @param time  the duration of record file
     * @param filepath  the path of record file
     */
    @Override
    public void showRecordState(MediaState state, String time, String filepath) {
        switch (state){
            case RUNNING:
                Log.d("info","Record is on,filepath:"+filepath);
                break;
            case IDLE:
                Log.d("info","Record is off,filepath"+filepath);
                if (!TextUtils.isEmpty(filepath)){
                    ToastUtil.showLongToast(mContext,"视频录制成功，视频保存在："+filepath);
                }
                break;
        }
    }

    /**
     * @param state
     * @param filepath the path of snapshot file
     */
    @Deprecated
    @Override
    public void showCaptureState(MediaState state, String filepath) {}

    /**
     * snapshot finished, send a broadcast to notify;
     */
    class CaptureBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String capture_path = intent.getStringExtra(DanalePlayer.EXTRA_SCREEN_SHOT_FILE_PATH); //the path of snapshot file
            Log.d("info","快照成功,地址："+capture_path);
            ToastUtil.showLongToast(mContext,"拍照成功，照片保存在："+capture_path);
        }
    }

    CaptureBroadcastReceiver captureBroadcastReceiver;

    private void registerReceiver(){
        if (captureBroadcastReceiver == null){
            captureBroadcastReceiver = new CaptureBroadcastReceiver();
            LocalBroadcastManager.getInstance(this).registerReceiver(captureBroadcastReceiver, new IntentFilter(DanalePlayer.ACTION_SCREEN_SHOT));
        }
    }

    private void unRegisterReceiver(){
        if (captureBroadcastReceiver != null){
            LocalBroadcastManager.getInstance(this).unregisterReceiver(captureBroadcastReceiver);
            captureBroadcastReceiver = null;
        }
    }
}
